#region Using Statements

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using NUnit.Framework;

#endregion

namespace Dotss.Client.Test.MsSqlDbServerLocator
{

    [TestFixture(Description="Creates a database on a local MsSqlExpress 2005 instance called 'DOTSS_UnitTest and attempts the install routines"), Explicit]
    public class InstallDb
    {

        #region Fields

        private string ConnString;

        #endregion

        #region Setup and Teardown

        [TestFixtureSetUp]
        public void SetupFixture()
        {

            SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();
            csb.DataSource = @"localhost\SQLEXPRESS";
            csb.InitialCatalog = "master";
            csb.Pooling = false;
            csb.IntegratedSecurity = true;

            ConnString = csb.ToString();

            using (SqlConnection conn = new SqlConnection(ConnString))
            {
                conn.Open();
                const string sql = "CREATE DATABASE DOTSS_UnitTest";
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;


                try
                {
                    cmd.ExecuteNonQuery();
                } catch (SqlException)
                {
                    // Do nothing, the database already exists
                }

            }

            csb.InitialCatalog = "DOTSS_UnitTest";

            // Inject to ensure that we are in a Test mode
            Dotss.Client.MsSqlDbServerLocator.SchemaVerificationRequired = false;
            Dotss.Client.MsSqlDbServerLocator.ConnectionString = csb.ToString();

        }

        [TestFixtureTearDown]
        public void Cleanup()
        {

            SqlConnectionStringBuilder csb =
                new SqlConnectionStringBuilder(Dotss.Client.MsSqlDbServerLocator.ConnectionString);
            csb.InitialCatalog = "master";
            using (SqlConnection conn = new SqlConnection(csb.ToString()))
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "DROP DATABASE DOTSS_UnitTest";
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }

            Dotss.Client.MsSqlDbServerLocator.ConnectionString = string.Empty;

        }

        #endregion

        #region Tests

        [Test]
        public void PatchFromVersion_Should_Create_SchemaVersion_Objects()
        {

            Dotss.Client.MsSqlDbServerLocator MSSQL = new Dotss.Client.MsSqlDbServerLocator();
            SqlConnection conn = Dotss.Client.MsSqlDbServerLocator.GetConnection();
            using (conn)
            {

                Version newVersion = MSSQL.PatchFromVersion(null, conn);
                Assert.AreEqual(Dotss.Client.MsSqlDbServerLocator.CURRENT_VERSION, newVersion, "Did not get patched to the current version");

                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "SELECT * FROM DOTSS.SchemaVersion ORDER BY CreateDate DESC";
                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    Console.Out.WriteLine("Version {0}:\t\t{1}", rdr["Version"], rdr["Description"]);
                }

            }

        }

        #endregion

    }

}
